package com.example.peanutsubway.web;

import com.alibaba.fastjson.JSONObject;
import com.example.peanutsubway.annotation.ResponseTypeAnnotation;
import com.example.peanutsubway.entity.AdminUserinfo;
import com.example.peanutsubway.entity.RoleInfo;
import com.example.peanutsubway.service.AdminUserService;
import com.example.peanutsubway.service.MenuInfoService;
import com.example.peanutsubway.service.RoleInfoService;
import com.example.peanutsubway.service.impl.AdminUserServiceImpl;
import com.example.peanutsubway.service.impl.MenuInfoServiceImpl;
import com.example.peanutsubway.service.impl.RoleInfoServiceImpl;
import com.example.peanutsubway.util.JDBCUtils;
import org.apache.commons.dbutils.DbUtils;

import javax.servlet.annotation.WebServlet;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Author：zzt
 * @Version：1.0
 * @Date：2021/10/13-21:10
 * @Since:jdk1.8
 * @Description:
 */
@WebServlet("/tree.do")
public class TreeServlet extends BaseServlet<AdminUserinfo> {
    private MenuInfoService menuInfoService = new MenuInfoServiceImpl();
    private AdminUserService adminUserService = new AdminUserServiceImpl();
    private RoleInfoService roleInfoService = new RoleInfoServiceImpl();

    @ResponseTypeAnnotation
    public String addTree(AdminUserinfo adminUserinfo) {
        String newRoleName = request.getParameter("newRoleName");
        if (adminUserinfo.getUsername() != null && !"".equals(adminUserinfo.getUsername())) {
            if (adminUserinfo.getRoleid() != -1) {
                return JSONObject.toJSONString(adminUserService.register(adminUserinfo));
            } else {
                //设置共同connection,事务一致
                Connection connection = JDBCUtils.getConnection();
                try {
                    connection.setAutoCommit(false);
                    roleInfoService.setConnection(connection);
                    menuInfoService.setConnection(connection);
                    adminUserService.setConnection(connection);

                    //新建角色权限树
                    Integer rid = menuInfoService.lastTreeId() + 1;
                    String treeData = request.getParameter("treeData");
                    String s1 = treeData.substring(1);
                    String s2 = s1.substring(0, s1.length() - 1);
                    String[] split = s2.split(",");
                    //添加角色菜单树
                    boolean addTree = false;
                    for (String s : split) {
                        addTree = menuInfoService.addTree(Integer.parseInt(s), rid);
                    }
                    //新建用户
                    adminUserinfo.setRoleid(rid);
                    boolean register = adminUserService.register(adminUserinfo);
                    //新建角色
                    String state = request.getParameter("state");
                    RoleInfo roleInfo = new RoleInfo(rid, newRoleName, Integer.parseInt(state));
                    boolean addRoleInfo = roleInfoService.addRoleInfo(roleInfo);
                    if (addRoleInfo && register && addTree) {
                        DbUtils.commitAndClose(connection);
                        return "true";
                    } else {
                        DbUtils.rollback(connection);
                    }
                } catch (Exception e) {
                    try {
                        DbUtils.rollback(connection);
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                } finally {
                    DbUtils.closeQuietly(connection);
                }
            }
        }
        return "false";
    }

    @ResponseTypeAnnotation
    public String TreeChecked() {
        String roleId = request.getParameter("roleid");
        //查询角色的树
        String[] roleMinId = menuInfoService.showUserMenuByRoleMinId(Integer.valueOf(roleId));
        return JSONObject.toJSONString(roleMinId);
    }

    @ResponseTypeAnnotation
    public String addTreeTwo() {
        Connection connection = JDBCUtils.getConnection();
        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        roleInfoService.setConnection(connection);
        menuInfoService.setConnection(connection);
        String newRoleName = request.getParameter("newRoleName");
        Integer rid = menuInfoService.lastTreeId() + 1;
        RoleInfo roleInfo = new RoleInfo(rid, newRoleName, 1);
        boolean addRoleInfo = roleInfoService.addRoleInfo(roleInfo);
        String treeData = request.getParameter("treeData");
        String s1 = treeData.substring(1);
        String s2 = s1.substring(0, s1.length() - 1);
        String[] split = s2.split(",");
        //添加角色菜单树
        boolean addTree = false;
        for (String s : split) {
            addTree = menuInfoService.addTree(Integer.parseInt(s), rid);
        }
        try {
            DbUtils.commitAndClose(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (addTree) {
            return "succeed";
        } else {
            return "false";
        }

    }

}
